package com.itboye.sunsun.person.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.itboye.sunsun.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

public class ApplyCustomActivity extends BaseActivity implements
		OnClickListener {

	private RelativeLayout backrela;
	private String ordercode;
	private Button submit;
	private EditText reasonedit;
	private String reason;
	ImageView check_1, check_2;
	EditText money_text;
	private String money;
	EditText edit_shuoming;
	private String totalPrice;
	boolean isTuiKuan = true;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setStatusBarColor(R.color.orange);
		setContentView(R.layout.activity_applycustomactivity);
		Intent intent = getIntent();
		ordercode = intent.getStringExtra("ordercode");
		totalPrice = intent.getStringExtra("price");
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>"+totalPrice);

	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.backrela) {
			this.finish();
		}
		if (v.getId() == R.id.submit) {

//			 Toast.makeText(this, totalPrice+"", 0).show();
			shouhou(ordercode);
		}
		if (v.getId() == R.id.check_1) {
			isTuiKuan = true;
			check_1.setBackgroundResource(R.drawable.checked);
			check_2.setBackgroundResource(R.drawable.unchecked);
		}

		if (v.getId() == R.id.check_2) {
			isTuiKuan = false;
			check_1.setBackgroundResource(R.drawable.unchecked);
			check_2.setBackgroundResource(R.drawable.checked);
		}

	}

	public void shouhou(String orderCode) {

		reason = reasonedit.getText().toString();

		if ("".equals(reason)) {
			Toast.makeText(this, "请输入原因", 0).show();
			return;
		}
		money = money_text.getText().toString();
		if ("".equals(money)) {
			Toast.makeText(this, "请输入退款金额", 0).show();
		} else if (Double.parseDouble(totalPrice) < Double.parseDouble(money)) {
			Toast.makeText(this, "金额必须小于订单总金额", 0).show();
		} else if (Float.parseFloat(money) < 0.01) {
			Toast.makeText(this, "退款金额必须大于等于0.01", 0).show();
		} else {
			MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
					.apiVer("100")
					.typeKey("BY_Orders_afterSale")
					.param("order_code", orderCode)
					.param("reason",
							"原因：" + reason + "金额：" + money + "说明："
									+ edit_shuoming.getText().toString()
									+ (isTuiKuan ? "退款" : "退货"))
					.param("uid",
							SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))
					.requestListener(new XRequestListener<String>() {

						@Override
						public void onResponse(String response) {

							Toast.makeText(App.ctx, response.toString(), 0)
									.show();
							ApplyCustomActivity.this.finish();
						}
					}).errorListener(new XErrorListener() {

						@Override
						public void onErrorResponse(Exception exception,
								int code, String msg) {
							if (exception instanceof CodeErrorException) {
								Toast.makeText(App.ctx, msg, 0).show();
							} else {
								Toast.makeText(App.ctx, exception.toString(), 0)
										.show();
							}

						}
					}).build();
			HttpRequest.getDefaultRequestQueue().add(request);
		}
	}

}
